package com.example.xjtuse_pc.mobilesafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xjtuse_pc.db.dao.BlackNumberDao;
import com.example.xjtuse_pc.domain.BlackNumberInfo;

import java.util.ArrayList;
import java.util.List;

public class BlackNumberActivity extends Activity {
    private ListView lv_black_number;
    private Myadopt myadopt;
    private List<BlackNumberInfo> infos;
    private BlackNumberDao bd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_black_number);
        lv_black_number = (ListView) findViewById(R.id.lv_black_number);
        infos = new ArrayList<BlackNumberInfo>();
        bd = new BlackNumberDao(BlackNumberActivity.this);
        infos = bd.findAll();
        myadopt = new Myadopt();
        lv_black_number.setAdapter(myadopt);
        //修改黑名单
        lv_black_number.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(BlackNumberActivity.this);
                final AlertDialog dialog = builder.create();
                View v = View.inflate(BlackNumberActivity.this, R.layout.dialog_blacknumber, null);
                et_setup_pwd = (EditText) v.findViewById(R.id.et_setup_pwd);
                et_setup_pwd.setEnabled(false);
                cb_number = (CheckBox) v.findViewById(R.id.cb_number);
                cb_sms = (CheckBox) v.findViewById(R.id.cb_sms);
                et_setup_pwd.setText(infos.get(position).getNumber());
                String mode = infos.get(position).getMode();
                if ("1".equals(mode)) {
                    cb_number.setChecked(true);
                } else if ("2".equals(mode)) {
                    cb_sms.setChecked(true);
                } else {
                    cb_number.setChecked(true);
                    cb_sms.setChecked(true);
                }

                ok = (Button) v.findViewById(R.id.ok);
                cancel = (Button) v.findViewById(R.id.cancel);
                dialog.setView(v, 0, 0, 0, 0);
                dialog.show();
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String blacknumber = et_setup_pwd.getText().toString().trim();
                        if (TextUtils.isEmpty(blacknumber)) {
                            Toast.makeText(getApplicationContext(), "黑名单号码不能为空", Toast.LENGTH_SHORT).show();
                        }
                        String mode;
                        if (cb_number.isChecked() && cb_sms.isChecked()) {
                            mode = "3";
                        } else if (cb_number.isChecked()) {
                            mode = "1";
                        } else if (cb_sms.isChecked()) {
                            mode = "2";
                        } else {
                            Toast.makeText(getApplicationContext(), "请选择拦截模式", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //添加数据到数据库
                        bd.updataMode(blacknumber, mode);
                        //更新listview
                        infos.get(position).setMode(mode);
                        //通知适配器
                        myadopt.notifyDataSetChanged();
                        dialog.dismiss();

                    }
                });
            }
        });

    }

    /**
     * 添加黑名单号码
     *
     * @param view
     */
    private EditText et_setup_pwd;
    private CheckBox cb_number, cb_sms;
    private Button ok, cancel;

    public void addBlackNumber(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(BlackNumberActivity.this);
        final AlertDialog dialog = builder.create();
        View v = View.inflate(BlackNumberActivity.this, R.layout.dialog_blacknumber, null);
        et_setup_pwd = (EditText) v.findViewById(R.id.et_setup_pwd);
        cb_number = (CheckBox) v.findViewById(R.id.cb_number);
        cb_sms = (CheckBox) v.findViewById(R.id.cb_sms);
        ok = (Button) v.findViewById(R.id.ok);
        cancel = (Button) v.findViewById(R.id.cancel);
        dialog.setView(v, 0, 0, 0, 0);
        dialog.show();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String blacknumber = et_setup_pwd.getText().toString().trim();
                if (TextUtils.isEmpty(blacknumber)) {
                    Toast.makeText(getApplicationContext(), "黑名单号码不能为空", Toast.LENGTH_SHORT).show();
                }
                String mode;
                if (cb_number.isChecked() && cb_sms.isChecked()) {
                    mode = "3";
                } else if (cb_number.isChecked()) {
                    mode = "1";
                } else if (cb_sms.isChecked()) {
                    mode = "2";
                } else {
                    Toast.makeText(getApplicationContext(), "请选择拦截模式", Toast.LENGTH_SHORT).show();
                    return;
                }
                //添加数据到数据库
                bd.add(blacknumber, mode);
                //更新listview
                BlackNumberInfo info = new BlackNumberInfo();
                info.setMode(mode);
                info.setNumber(blacknumber);
                infos.add(0, info);
                //通知适配器
                myadopt.notifyDataSetChanged();
                dialog.dismiss();

            }
        });

    }


    private class Myadopt extends BaseAdapter {

        @Override
        public int getCount() {
            return infos.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            final ViewHolder holder;
            //1.减少内存中view对象创建的个数
            if (convertView == null) {
                view = View.inflate(BlackNumberActivity.this, R.layout.list_item_blacknumber, null);
                holder = new ViewHolder();
                //2.减少子孩子查询的次数  内存中对象的地址。
                holder.tv_number = (TextView) view.findViewById(R.id.tv_black_number);
                holder.tv_mode = (TextView) view.findViewById(R.id.tv_block_mode);
                holder.iv_number_delete = (ImageView) view.findViewById(R.id.iv_number_delete);
                //当孩子生出来的时候找到他们的引用，存放在记事本，放在父亲的口袋
                view.setTag(holder);//提高5%

            } else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }
            holder.tv_number.setText(infos.get(position).getNumber());
            String mode = infos.get(position).getMode();
            if ("1".equals(mode)) {
                holder.tv_mode.setText("电话拦截");
            } else if ("2".equals(mode)) {
                holder.tv_mode.setText("短信拦截");
            } else {
                holder.tv_mode.setText("全部拦截");
            }
            holder.iv_number_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(BlackNumberActivity.this);
                    builder.setTitle("警告");
                    builder.setMessage("确定要删除这条记录么？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //数据库删除此数据
                            bd.delete(infos.get(position).getNumber());
                            //更新界面
                            infos.remove(position);
                            //更新listview
                            myadopt.notifyDataSetChanged();
                        }
                    });
                    builder.setNegativeButton("取消", null);
                    builder.show();
                }
            });
//            view.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(getApplicationContext(), "list被点击", Toast.LENGTH_SHORT).show();
//                }
//            });
            return view;
        }
    }

    /**
     * view对象的容器
     * 记录孩子的内存地址。
     * 相当于一个记事本
     */
    static class ViewHolder {
        TextView tv_number;
        TextView tv_mode;
        ImageView iv_number_delete;
    }

}
